home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / seexec.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.8 KB  |  213 lines

  1. /* Copyright (C) 1994, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: seexec.c,v 1.3 2000/09/19 19:00:49 lpd Exp $ */
  20. /* eexec filters */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "strimpl.h"
  23. #include "sfilter.h"
  24. #include "gscrypt1.h"
  25. #include "scanchar.h"
  26.  
  27. /* ------ eexecEncode ------ */
  28.  
  29. /* Encoding is much simpler than decoding, because we don't */
  30. /* worry about initial characters or hex vs. binary (the client */
  31. /* has to take care of these aspects). */
  32.  
  33. private_st_exE_state();
  34.  
  35. /* Process a buffer */
  36. private int
  37. s_exE_process(stream_state * st, stream_cursor_read * pr,
  38.           stream_cursor_write * pw, bool last)
  39. {
  40.     stream_exE_state *const ss = (stream_exE_state *) st;
  41.     const byte *p = pr->ptr;
  42.     byte *q = pw->ptr;
  43.     uint rcount = pr->limit - p;
  44.     uint wcount = pw->limit - q;
  45.     uint count;
  46.     int status;
  47.  
  48.     if (rcount <= wcount)
  49.     count = rcount, status = 0;
  50.     else
  51.     count = wcount, status = 1;
  52.     gs_type1_encrypt(q + 1, p + 1, count, (crypt_state *)&ss->cstate);
  53.     pr->ptr += count;
  54.     pw->ptr += count;
  55.     return status;
  56. }
  57.  
  58. /* Stream template */
  59. const stream_template s_exE_template = {
  60.     &st_exE_state, NULL, s_exE_process, 1, 2
  61. };
  62.  
  63. /* ------ eexecDecode ------ */
  64.  
  65. private_st_exD_state();
  66.  
  67. /* Set defaults. */
  68. private void
  69. s_exD_set_defaults(stream_state * st)
  70. {
  71.     stream_exD_state *const ss = (stream_exD_state *) st;
  72.  
  73.     ss->binary = -1;        /* unknown */
  74.     ss->lenIV = 4;
  75.     ss->record_left = max_long;
  76.     ss->hex_left = max_long;
  77.     /* Clear pointers for GC */
  78.     ss->pfb_state = 0;
  79. }
  80.  
  81. /* Initialize the state for reading and decrypting. */
  82. /* Decrypting streams are not positionable. */
  83. private int
  84. s_exD_init(stream_state * st)
  85. {
  86.     stream_exD_state *const ss = (stream_exD_state *) st;
  87.  
  88.     ss->odd = -1;
  89.     ss->skip = ss->lenIV;
  90.     return 0;
  91. }
  92.  
  93. /* Process a buffer. */
  94. private int
  95. s_exD_process(stream_state * st, stream_cursor_read * pr,
  96.           stream_cursor_write * pw, bool last)
  97. {
  98.     stream_exD_state *const ss = (stream_exD_state *) st;
  99.     const byte *p = pr->ptr;
  100.     byte *q = pw->ptr;
  101.     int skip = ss->skip;
  102.     int rcount = pr->limit - p;
  103.     int wcount = pw->limit - q;
  104.     int status = 0;
  105.     int count = (wcount < rcount ? (status = 1, wcount) : rcount);
  106.  
  107.     if (ss->binary < 0) {
  108.     /*
  109.      * This is the very first time we're filling the buffer.
  110.      * Determine whether this is ASCII or hex encoding.
  111.      */
  112.     const byte *const decoder = scan_char_decoder;
  113.     int i;
  114.  
  115.     if (rcount < 8)
  116.         return 0;
  117.     /*
  118.      * Adobe's documentation doesn't actually specify the test
  119.      * that eexec should use, but we believe the following
  120.      * gives correct answers even on certain non-conforming
  121.      * PostScript files encountered in practice:
  122.      */
  123.     ss->binary = 0;
  124.     for (i = 1; i <= 8; i++)
  125.         if (!(decoder[p[i]] <= 0xf ||
  126.           decoder[p[i]] == ctype_space)
  127.         ) {
  128.         ss->binary = 1;
  129.         if (ss->pfb_state != 0) {
  130.             /* Stop at the end of the .PFB binary data. */
  131.             ss->record_left = ss->pfb_state->record_left;
  132.         }
  133.         break;
  134.         }
  135.     }
  136.     if (ss->binary) {
  137.     if (count > ss->record_left) {
  138.         count = ss->record_left;
  139.         status = 0;
  140.     }
  141.     /*
  142.      * We pause at the end of the .PFB binary data,
  143.      * in an attempt to keep from reading beyond the end of
  144.      * the encrypted data.
  145.      */
  146.     if ((ss->record_left -= count) == 0)
  147.         ss->record_left = max_long;
  148.     pr->ptr = p + count;
  149.     } else {
  150.     /*
  151.      * We only ignore leading whitespace, in an attempt to
  152.      * keep from reading beyond the end of the encrypted data;
  153.      * but some badly coded files require us to ignore % also.
  154.      */
  155.     stream_cursor_read r;
  156.     const byte *start;
  157.  
  158. hp:    r = *pr;
  159.     start = r.ptr;
  160.     if (r.limit - r.ptr > ss->hex_left)
  161.         r.limit = r.ptr + ss->hex_left;
  162.     status = s_hex_process(&r, pw, &ss->odd,
  163.                    hex_ignore_leading_whitespace);
  164.     pr->ptr = r.ptr;
  165.     ss->hex_left -= r.ptr - start;
  166.     /*
  167.      * Check for having finished a prematurely decoded hex section of
  168.      * a PFB file.
  169.      */
  170.     if (ss->hex_left == 0)
  171.         ss->binary = 1;
  172.     count = pw->ptr - q;
  173.     if (status < 0 && ss->odd < 0) {
  174.         if (count) {
  175.         --p;
  176.         status = 0;    /* reprocess error next time */
  177.         } else if (*p == '%')
  178.         goto hp;    /* ignore % */
  179.     }
  180.     p = q;
  181.     }
  182.     if (skip >= count && skip != 0) {
  183.     gs_type1_decrypt(q + 1, p + 1, count,
  184.              (crypt_state *) & ss->cstate);
  185.     ss->skip -= count;
  186.     count = 0;
  187.     status = 0;
  188.     } else {
  189.     gs_type1_decrypt(q + 1, p + 1, skip,
  190.              (crypt_state *) & ss->cstate);
  191.     count -= skip;
  192.     gs_type1_decrypt(q + 1, p + 1 + skip, count,
  193.              (crypt_state *) & ss->cstate);
  194.     ss->skip = 0;
  195.     }
  196.     pw->ptr = q + count;
  197.     return status;
  198. }
  199.  
  200. /* Stream template */
  201. /*
  202.  * The specification of eexec decoding requires that it never read more than
  203.  * 512 source bytes ahead.  The only reliable way to ensure this is to
  204.  * limit the size of the output buffer to 256.  We set it a little smaller
  205.  * so that it will stay under the limit even after adding min_in_size
  206.  * for a subsequent filter in a pipeline.  Note that we have to specify
  207.  * a size of at least 128 so that filter_read won't round it up.
  208.  */
  209. const stream_template s_exD_template = {
  210.     &st_exD_state, s_exD_init, s_exD_process, 8, 200,
  211.     NULL, s_exD_set_defaults
  212. };
  213.